package com.hyy.taobao.listener;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

@WebListener
public class MaxLoginCountListener implements HttpSessionListener {
    @Override
    public void sessionCreated(HttpSessionEvent se) {
        ServletContext servletContext = se.getSession().getServletContext();
        int onlineCount = (int) servletContext.getAttribute("onlineCount");
        onlineCount++;
        servletContext.setAttribute("onlineCount",onlineCount);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        ServletContext servletContext = se.getSession().getServletContext();
        int onlineCount = (int) servletContext.getAttribute("onlineCount");
        onlineCount--;
        servletContext.setAttribute("onlineCount",onlineCount);
    }
}
